package com.atguigu.serviceEdu.controller;


import com.atguigu.commonutil.ApiResponse;
import com.atguigu.commonutil.ResultCode;
import com.atguigu.serviceEdu.entity.EduTeacher;
import com.atguigu.serviceEdu.entity.vo.TeacherQuery;
import com.atguigu.serviceEdu.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Aug28-tlsjn
 * @since 2020-06-29
 */
@RestController
@Api(value = "讲师管理")
@RequestMapping("/serviceEdu/teacher")
@CrossOrigin
public class EduTeacherController {



    @Resource
    private EduTeacherService teacherService;

    private ApiResponse<Map<String, Object>> pageQuery(Page<EduTeacher> page, QueryWrapper<EduTeacher> queryWrapper) {
        teacherService.page(page, queryWrapper);

        Map<String, Object> map = new HashMap<>(5);
        map.put("current", page.getCurrent());
        map.put("size", page.getSize());
        map.put("total", page.getTotal());
        map.put("pages", page.getPages());
        map.put("records", page.getRecords());
        return ApiResponse.successInstance().setData(map);
    }

    @ApiOperation(value = "带分页的组合条件查询")
    @PostMapping(value = "/get/page/conditional/{current}/{size}")
    public ApiResponse<Map<String, Object>> conditionalPage(@ApiParam(value = "要看第几页？", required = true) @PathVariable("current") long current,
                                       @ApiParam(value = "每页最多显示几条记录？", required = true) @PathVariable("size") long size,
                                       @ApiParam(value = "封装查询条件的对象") @RequestBody(required = false) TeacherQuery queryCondition) {


        Page<EduTeacher> teacherPage = new Page<>(current, size);

        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();

        String conditionName = queryCondition.getName();
        Integer conditionLevel = queryCondition.getLevel();
        String conditionBeginTime = queryCondition.getBeginTime();
        String conditionEndTime = queryCondition.getEndTime();

        if (!StringUtils.isEmpty(conditionName)) {
            queryWrapper.like("name", conditionName);
        }

        if (conditionLevel != null) {
            queryWrapper.eq("level", conditionLevel);
        }

        if (!StringUtils.isEmpty(conditionBeginTime)) {
            queryWrapper.ge("gmt_create", conditionBeginTime);
        }

        if (!StringUtils.isEmpty(conditionEndTime)) {
            queryWrapper.le("gmt_create", conditionEndTime);
        }

        return pageQuery(teacherPage, queryWrapper);

    }

    /**
     * 测试环境：查询所有讲师
     */
    @ApiOperation(value = "查询所有讲师")
    @GetMapping(value = "/getAll")
    public ApiResponse<List<EduTeacher>> getAll() {
        List<EduTeacher> eduTeachers = teacherService.list();
        return new ApiResponse<>(true, ResultCode.SUCCESS, "请求成功", eduTeachers);
    }

    @ApiOperation(value = "根据 id 查询某个讲师信息")
    @GetMapping(value = "/get/{id}")
    public ApiResponse<EduTeacher> getOne(@ApiParam(value = "要查询的讲师的id", required = true) @PathVariable("id") String id) {
        EduTeacher eduTeacher = teacherService.getById(id);
        // int i = 1 / 0;
        return new ApiResponse<>(true, ResultCode.SUCCESS, "请求成功", eduTeacher);
    }

    @ApiOperation(value = "修改讲师的信息")
    @PostMapping(value = "/edit")
    public ApiResponse<?> editTeacher(@ApiParam("要修改的讲师信息") @RequestBody EduTeacher eduTeacher) {
        boolean update = teacherService.update(eduTeacher, null);
        if (update) {
            return new ApiResponse<>(true, ResultCode.SUCCESS, "请求成功", teacherService.getById(eduTeacher.getId()));
        } else {
            return ApiResponse.failedInstance();
        }
    }

    /**
     * 分页查询
     * @param current 要查的当前页
     * @param size 每页展示的数据数量
     * @return 分页查询的结果
     */
    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/get/page/{current}/{size}")
    public ApiResponse<Map<String, Object>> getByPage(@ApiParam(value = "要看第几页？", required = true) @PathVariable("current") long current,
                                 @ApiParam(value = "每页最多显示几条记录？", required = true) @PathVariable("size") long size) {
        Page<EduTeacher> pageTeacher = new Page<>(current, size);

        return pageQuery(pageTeacher, null);
    }

    /**
     * 逻辑删除
     */
    @ApiOperation(value = "根据传入的 id 删除指定的讲师信息")
    @DeleteMapping(value = "/{id}")
    public ApiResponse<?> deleteTeacherById(@ApiParam(required = true, name = "id", value = "讲师的id")
                                        @PathVariable("id")
                                                String id) {
        boolean remove = teacherService.removeById(id);
        if (remove) {
            return ApiResponse.successInstance().setMessage("删除成功~O(∩_∩)O");
        } else {
            return ApiResponse.failedInstance().setMessage("删除失败！/(ㄒoㄒ)/~~");
        }
    }

    @ApiOperation(value = "添加讲师信息")
    @PostMapping(value = "/save")
    public ApiResponse<?> saveTeacher(@ApiParam(value = "要添加的讲师信息") @RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);
        if (save) {
            return new ApiResponse<>(true, ResultCode.SUCCESS, "保存成功", eduTeacher);
        } else {
            return ApiResponse.failedInstance().setMessage("保存失败！/(ㄒoㄒ)/~~");
        }
    }
}

